<?php

//namespace models;

class News extends Record {	
	protected $table = Options::DB_TABLE_NEWS;
	public function url(){
		return URL::action('NewsController@showDetail', [
			'id'=> 2, //$this->id,
			'title'=> $this->sefFormat('something test'), //$this->title,
		]);
	}
	
	public function scopeOrderByMostViewed($query,$criteria) {		
		return $query->orderBy('noOfViews', $criteria );
	}
	public function scopeOrderByMostCommented($query,$criteria) {
		return $query->orderBy('noOfComments', $criteria );
	}
	public function scopeOrderByCreated($query,$criteria) {
		return $query->orderBy(Options::DB_TABLE_NEWS.'.created_at', $criteria );
	}
	public function scopeLanguage($query) {
		return $query->leftJoin(Options::DB_TABLE_CATEGORIES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.id', '=', Options::DB_TABLE_NEWS.'.categoryID');
				    })->leftJoin(Options::DB_TABLE_LANGUAGES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.languageID', '=', Options::DB_TABLE_LANGUAGES.'.id');
				    })->where(Options::DB_TABLE_LANGUAGES.'.code', '=', Config::get('app.locale') );
	}	
	public function scopeCategoryID($query, $criteria) {
		return $query->where(Options::DB_TABLE_NEWS.'.categoryID', '=', $criteria);
	}
	public function scopeCategoryName($query, $criteria) {
		return $query->where(Options::DB_TABLE_CATEGORIES.'.name', '=', $criteria);
	}
	public function scopeIsTopNews($query, $criteria) {
		return $query->where(Options::DB_TABLE_NEWS.'.isTopStory', '=', $criteria);
	}
	public function scopeNewsID($query, $criteria) {
		return $query->where(Options::DB_TABLE_NEWS.'.id', '=', $criteria);
	}
	public function scopeRecordBefore($query, $criteria) {
		return $query->where(Options::DB_TABLE_NEWS.'.id', '<', $criteria);
	}
	public function scopeRecordAfter($query, $criteria) {
		return $query->where(Options::DB_TABLE_NEWS.'.id', '>', $criteria);
	}
	public function scopeJoinUsers($query) {
		return $query->leftJoin(Options::DB_TABLE_USERS, function($join) {
		      $join->on(Options::DB_TABLE_NEWS.'.created_by', '=', Options::DB_TABLE_USERS.'.id');
		    });
	}
	//Top Story
	public function scopeTopStory($query,$criteria){
		return $query->where(Options::DB_TABLE_NEWS.'.isTopStory', '=', $criteria);
	}
	//latest categry news
	
	public function scopeLatestCaNew($query,$criteria){
		return $query->where(Options::DB_TABLE_NEWS.'.categoryID', '=', $criteria);
	}
	//
	public function scopeMostViewedAll($query,$criteria){
		//echo $criteria; 		
		return $query->orderBy('noOfViews', $criteria );
	}
	public function addNew(){
		return $this->save();
	}
	
	public function getListNewsById(){
		return News::where('status','<>','-1')
				->where('categoryID','=','44')
				->orderBy('id', 'asc')
				->lists('id');
	}
	
}

